package Box2D.test
{
import Box2D.Collision.*;
import Box2D.Collision.Shapes.*;
import Box2D.Common.Math.*;
import Box2D.Dynamics.*;
import Box2D.Dynamics.Controllers.*;
import Box2D.Dynamics.Joints.*;

import assets.avatar.ac.AC10000Asset;

import com.waitingsnow.core.display.player.BmpDataPlayer;
import com.waitingsnow.core.util.display.BmpDataUtil;

import flash.display.Sprite;
import flash.events.*;

public class TestBox2d extends Sprite
{
	
	//程序入口，创建一个世界
	private var world:b2World = new b2World(new b2Vec2(0,10.0),true);
	
	private var worldScale:int = 30;//30像素=1米
	private  static const vesion:String="box2d for 2.1a";
	
	private var mouseJoint:b2MouseJoint;
	public function TestBox2d()
	{
		var ac:AC10000Asset = new AC10000Asset();
		var avatarBmp:BmpDataPlayer = new BmpDataPlayer();
		avatarBmp.repeatCount = 0;
		avatarBmp.bmpDataArr = BmpDataUtil.getBmpDataArr(ac);
		avatarBmp.play();
		addChild(avatarBmp);
		
		
//		debugDraw();
//		/*drawBox(250,300,500,50,false);*/
//		drawBox(250,100,50,50,true);
//		addEventListener(Event.ENTER_FRAME, updateHandler);
		
	}
	
	//绘制一个矩形
	private function drawBox(x:Number,y:Number,width:Number,height:Number,isDynamic:Boolean):void
	{
		var bodyDef:b2BodyDef= new b2BodyDef();
		bodyDef.position.Set(x/worldScale, y/worldScale);
		if (isDynamic)
		{
			bodyDef.type = b2Body.b2_dynamicBody;
		}
		var box:b2PolygonShape = new b2PolygonShape();
		box.SetAsBox(width/2/worldScale, height/2/worldScale);
		
		var fixtureDef:b2FixtureDef = new b2FixtureDef();
		fixtureDef.shape = box;
		fixtureDef.density = 0;
		fixtureDef.friction = 0.5;
		
		//使用自定义图形的贴图
		bodyDef.userData=new Sprite();
		bodyDef.userData.width = width; 
		bodyDef.userData.height = height;
		addChild(bodyDef.userData);
		var body:b2Body = world.CreateBody(bodyDef);
		body.CreateFixture(fixtureDef);
	}
	
	
	private function debugDraw():void
	{
		var _debugDraw:b2DebugDraw=new b2DebugDraw();
		var sprite:Sprite=new Sprite();
		addChild(sprite);
		_debugDraw.SetSprite(sprite);
		_debugDraw.SetDrawScale(worldScale);
		_debugDraw.SetFlags(b2DebugDraw.e_shapeBit);
		world.SetDebugDraw(_debugDraw);
		world.DrawDebugData();
	}
	
	private function updateHandler(event:Event):void
	{
		world.Step(1/30,10,10);
		world.ClearForces();
		//world.DrawDebugData();
		for (var bb:b2Body = world.GetBodyList(); bb; bb = bb.GetNext()){
			if (bb.GetUserData() is Sprite){
				var sprite:Sprite = bb.GetUserData() as Sprite;
				sprite.x = bb.GetPosition().x * 30;
				sprite.y = bb.GetPosition().y * 30;
				sprite.rotation = bb.GetAngle() * (180/Math.PI);
			}
		}
		
	}
}
}


//package Box2D.test
//{
//import Box2D.Collision.Shapes.b2PolygonShape;
//import Box2D.Common.Math.b2Vec2;
//import Box2D.Dynamics.b2Body;
//import Box2D.Dynamics.b2BodyDef;
//import Box2D.Dynamics.b2DebugDraw;
//import Box2D.Dynamics.b2FixtureDef;
//import Box2D.Dynamics.b2World;
//
//import flash.display.Sprite;
//import flash.display.StageScaleMode;
//import flash.events.Event;
//
//[SWF(backgroundColor="0x333333",width="800",height="600",frameRate="30")]
//public class TestBox2d extends Sprite
//{
//	private const PIXELS_TO_METRE:int = 50;
//	private var _world:b2World;
//	public function TestBox2d()
//	{
//		stage.scaleMode = StageScaleMode.NO_SCALE;
//		
//		//定义重力向量
//		var gravity:b2Vec2 = new b2Vec2(0,10);
//		//实例化世界（接受两个参数重力向量和当物体停止移动时是否允许物体休眠，一个休眠中的物体不需要任何模拟）
//		_world = new b2World(gravity,true);
//		
//		//实例化刚体定义数据
//		var groundBodyDef:b2BodyDef= new b2BodyDef();
//		//设置刚体的初始坐标在1，1米处
//		groundBodyDef.position.Set(1,1);
//		groundBodyDef.type = b2Body.b2_dynamicBody;
//		
//		//实例化一个刚体（将刚体定义数据交给世界能自动创建刚体）
//		var groundBody:b2Body = _world.CreateBody(groundBodyDef);
//		
//		//实例化刚体的形状
//		var groundBox:b2PolygonShape = new b2PolygonShape();
//		//将形状定义成2X2的方形
//		groundBox.SetAsBox(1,1);
//		
//		//实例化粘物
//		var groundFixtureDef:b2FixtureDef = new b2FixtureDef();
//		//粘上形状。。。
//		groundFixtureDef.shape = groundBox;
//		//让刚体表现出粘物绑定形状的形状
//		groundBody.CreateFixture(groundFixtureDef);
//		
//		/**
//		 * 以上的工作只是定义好必要的数据，但还是需要下面相关的代码来渲染出来
//		 * 绘图相关
//		 */
//		var debugSprite:Sprite = new Sprite();
//		addChild(debugSprite);
//		var debugDraw:b2DebugDraw = new b2DebugDraw();
//		debugDraw.SetSprite(debugSprite);
//		debugDraw.SetLineThickness( 1.0);
//		debugDraw.SetAlpha(1);
//		debugDraw.SetFillAlpha(0.4);
//		//设置缩放量
//		debugDraw.SetDrawScale(PIXELS_TO_METRE);
//		//设置绘图模式
//		debugDraw.SetFlags(b2DebugDraw.e_shapeBit);
//		_world.SetDebugDraw(debugDraw);
//		
//		//            _world.DrawDebugData();
//		
//		addEventListener(Event.ENTER_FRAME,updateFrame);
//	}
//	
//	protected function updateFrame(event:Event):void
//	{
//		var timeStep:Number = 1/ 30;
//		var velocityIterations:int = 8;
//		var positionIterations:int = 3;
//		
//		_world.Step(timeStep,velocityIterations,positionIterations);
//		_world.ClearForces();
//		_world.DrawDebugData();
//	}
//}
//}